﻿using System.Web.Mvc;
using AutoClub.Core.Dao;
using AutoClub.Core.Dao.Implementations;
using AutoClub.Core.Domain.Dao;
using AutoClub.Models;
using Microsoft.Practices.ServiceLocation;
using Microsoft.Practices.Unity;

namespace AutoClub.Support
{
    /// <summary>
    /// Inicializacion de la registracion de dependencias
    /// </summary>
    public class AutoClubBootstrapper
    {
        #region Statics

        private static AutoClubBootstrapper instance = new AutoClubBootstrapper();

        public static void RegisterAll()
        {
            IUnityContainer container = instance.GetUnityContainer();
            DependencyResolver.SetResolver(new UnityDependencyResolver(container));
            ServiceLocator.SetLocatorProvider(() => new UnityServiceLocator(container));
        }

        #endregion

        #region Constructor

        /// <summary>
        /// Constructor privado, encapsulado a una unica instancia.
        /// </summary>
        private AutoClubBootstrapper()
        {

        }

        #endregion

        private IUnityContainer GetUnityContainer()
        {
            //Create UnityContainer          
            IUnityContainer container = new UnityContainer()
                        .RegisterType<IControllerActivator, CustomControllerActivator>()
                        .RegisterType<IUnitOfWork, AutoClubUnitOfWork>()
                        .RegisterType<IUnitOfWork, AutoClubUnitOfWork>(new HttpContextLifetimeManager<IUnitOfWork>())
                        .RegisterType<IUsersRepo, UsersRepo>(new HttpContextLifetimeManager<IUsersRepo>())
                        .RegisterType<IUserRolesRepo, UserRolesRepo>(new HttpContextLifetimeManager<IUserRolesRepo>())
                        .RegisterType<ICarCategoriesRepo, CarCategoriesRepo>(new HttpContextLifetimeManager<ICarCategoriesRepo>())
                        .RegisterType<IRaceRepo, RaceRepo>(new HttpContextLifetimeManager<IRaceRepo>())
                        .RegisterType<IModelMapper, AutoModelMapper>(new HttpContextLifetimeManager<IModelMapper>());


            return container;
        }


    }
}